//
//  RRadioPlayerController.m
//  EPAa
//
//  Created by lanou3g on 16/4/21.
//  Copyright © 2016年 李日青. All rights reserved.
//

#import "RRadioPlayerController.h"
#import "RRadioListModel.h"
#import "RAVManager.h"
@interface RRadioPlayerController ()
//标题
@property (weak, nonatomic) IBOutlet UILabel *titleLabel;
//封面
@property (weak, nonatomic) IBOutlet UIImageView *coverImageView;
//当前时间
@property (weak, nonatomic) IBOutlet UILabel *currentLabel;
//播放进度
@property (weak, nonatomic) IBOutlet UISlider *reatSlider;
//暂停播放
@property (weak, nonatomic) IBOutlet UIButton *playButton;

//总时间
@property (weak, nonatomic) IBOutlet UILabel *totalLabel;

//计时器
@property (nonatomic, strong)NSTimer *playTimer;
@end

@implementation RRadioPlayerController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationController.navigationBar.translucent = NO;
    RRadioListModel *model = self.radioArray[self.currentIndex];
    self.titleLabel.text = model.title;
    [self.coverImageView sd_setImageWithURL:[NSURL URLWithString:model.coverimg]];
    [RAVManager shareManager].currentIndex = self.currentIndex;
    NSMutableArray *itemArray = [NSMutableArray new];
    for (RRadioListModel *model in self.radioArray) {
        NSURL *musicUrl = [NSURL URLWithString:model.playInfoModel.musicUrl];
        AVPlayerItem *item = [AVPlayerItem playerItemWithURL:musicUrl];
        [itemArray addObject:item];
    }
    self.playTimer = [NSTimer scheduledTimerWithTimeInterval:0 target:self selector:@selector(timerAction) userInfo:nil repeats:YES];
    //AVPlayerItem数组
    [RAVManager shareManager].itemArray = itemArray;
    [[RAVManager shareManager] play];
}
- (void)timerAction {
    float current = [RAVManager shareManager].player.currentTime.value/[RAVManager shareManager].player.currentTime.timescale;
    NSString *currrentStr = [NSString stringWithFormat:@"%02ld:%02ld",(NSInteger)current/60,(NSInteger)current%60];
    self.currentLabel.text = currrentStr;
    float total = 0;
    if ([RAVManager shareManager].player.currentItem.duration.timescale > 0) {
      total = [RAVManager shareManager].player.currentItem.duration.value/[RAVManager shareManager].player.currentItem.duration.timescale;

    }
    
    NSString *totalStr = [NSString stringWithFormat:@"%02ld:%02ld",(NSInteger)total/60,(NSInteger)total%60];
    self.totalLabel.text = totalStr;
    if (total > 0) {
        float rate = current/total;
        self.reatSlider.value = rate;
    }
}

//上一曲
- (IBAction)didClickPreviousButton:(id)sender {
    [[RAVManager shareManager] previous];
    //图片和标题
    self.currentIndex--;
    if (self.currentIndex < 0) {
        self.currentIndex = self.radioArray.count - 1;
    }
    RRadioListModel *model = self.radioArray[self.currentIndex];
    [self.coverImageView sd_setImageWithURL:[NSURL URLWithString:model.coverimg]];
    self.titleLabel.text = model.title;
    [self.playButton setImage:[UIImage imageNamed:@"播放.png"] forState:UIControlStateNormal];
}
//播放，暂停
- (IBAction)didClickPlayerButton:(id)sender {
    if ([RAVManager shareManager].player.rate == 0) {
        [[RAVManager shareManager] play];
        [(UIButton *)sender setImage:[UIImage imageNamed:@"播放.png"] forState:UIControlStateNormal];
        
    }else {
        [[RAVManager shareManager] pause];
         [(UIButton *)sender setImage:[UIImage imageNamed:@"暂停-1.png"] forState:UIControlStateNormal];
    }
}
//下一曲
- (IBAction)didClickNextButton:(id)sender {
    [[RAVManager shareManager] next];
    //图片标题
    self.currentIndex++;
    if (self.currentIndex > self.radioArray.count) {
        self.currentIndex = 0;
       
        
    }
    RRadioListModel *model = self.radioArray[self.currentIndex];
    [self.coverImageView sd_setImageWithURL:[NSURL URLWithString:model.coverimg]];
    self.titleLabel.text = model.title;
     [self.playButton setImage:[UIImage imageNamed:@"播放.png"] forState:UIControlStateNormal];
}
//改变播放进度
- (IBAction)didChangeRateAction:(UISlider *)sender {
    CMTime changeTime = CMTimeMake(sender.value *[RAVManager shareManager].player.currentItem.duration.value, [RAVManager shareManager].player.currentItem.duration.timescale);
    [[RAVManager shareManager] seekToTime:changeTime];
}




- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
